<?php
$company_id = isset($_REQUEST['company_id']) ? $_REQUEST['company_id'] : 0;
// Check permissions
if (!$canEdit) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}
$q  = new DBQuery;
$q->addTable('billingcode','bc');
$q->addQuery('billingcode_id, billingcode_name, billingcode_value, billingcode_desc, billingcode_status');
$q->addOrder('billingcode_name ASC');
//$q->addWhere('bc.billingcode_status = 0');
$q->addWhere('company_id = ' . $company_id);
$billingcodes = $q->loadList();
$q->clear();

$q  = new DBQuery;
$q->addTable('companies','c');
$q->addQuery('company_id, company_name');
$q->addOrder('company_name ASC');
$company_list = $q->loadHashList();
$company_list[0] = $AppUI->_('...');
$q->clear();

$company_name = $company_list[$company_id];

$titleBlock = new CTitleBlock( 'Edit Billing Codes' );
$titleBlock->addButton('Main page', '?m=system');
$titleBlock->show();
?>
<script type="text/javascript" language="javascript">

function doEditCode(id) {
	var r = $('idCode'+id);
	var f = document.CodeEditFrom;
	if (r) {
		f.billingcode_name.value = r.cells[1].innerHTML;
		f.billingcode_value.value = r.cells[2].innerHTML;
		f.billingcode_desc.value = r.cells[3].innerHTML;
	}
	else {
		f.billingcode_name.value = '';
		f.billingcode_value.value = '';
		f.billingcode_desc.value = '';
	}
	f.billingcode_id.value = id;
	f.company_id.value = <?=$company_id?>;

	Dialog('idCodeEdit').show();
	f.billingcode_name.select();
	f.billingcode_name.focus();
}

function doDeleteCode(id) {
	document.frmDel.billingcode_id.value = id;
	document.frmDel.submit();
}

</script>

<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
	<td class="tabon">Billing Codes</td>
	<td width="100%"></td>
</tr>
</table>
<table width="100%" border="0" cellpadding="7" cellspacing="0" class="infopanel">
<tr>
<td class="status">Company:</td>
<td>
<form name="frmChangeCompany" action="index.php?m=system&a=billingcode" method="post">
<?=arraySelect( $company_list, 'company_id', 'size="1" class="text" onchange="this.form.submit()"', $company_id, false )?>
</form>
<td>
<td width="100%">&nbsp;</td>
<td><input type="button" class="button" value="New code" onclick="doEditCode(0)"/></td>
</tr>
</table>

<table width="100%" border="0" cellpadding="2" cellspacing="0" class="list">
<tr>
	<th width="12">&nbsp;</th>
	<th width="60"><?=$AppUI->_('Code')?></th>
	<th width="60"><?=$AppUI->_('Value')?></th>
	<th><?=$AppUI->_('Description')?></th>
	<th width="12" class="last">&nbsp;</th>
</tr>

<? foreach($billingcodes as $code) { ?>
<tr id="idCode<?=$code['billingcode_id']?>" class="second">
	<td>
		<a href="javascript:doEditCode(<?=$code['billingcode_id']?>)"
			title="<?=$AppUI->_('edit')?>"><img src="images/edit.gif" title="Edit"/></a>
	</td>
	<td align="left"><?=$code['billingcode_name'] . ($code['billingcode_status'] == 1 ? ' (deleted)' : '')?></td>
	<td align="center"><?=$code['billingcode_value']?></td>
	<td><?=$code['billingcode_desc']?></td>
	<? if ($a['billingcode_status'] == 0) ?>
	<td><a href="javascript:doDeleteCode(<?=$code['billingcode_id']?>)"
			title="<?=$AppUI->_('delete')?>"><img src="images/common_delete.gif" title="Delete" /></a>
	</td>
</tr>
<? } ?>
</table>

<div id="idCodeEdit" class="popup" style="visibility: hidden" autohide="click">
<form name="CodeEditFrom" action="index.php?m=system" method="post">
	<input type="hidden" name="dosql" value="do_billingcode_aed" />
	<input type="hidden" name="del" value="0" />
	<input type="hidden" name="company_id" value="0" />
	<input type="hidden" name="billingcode_status" value="0" />
	<input type="hidden" name="billingcode_id" value="0" />

<table border="0" cellpadding="4" cellspacing="0" width="200px">
<tr>
	<th colspan="2" bgcolor="#E0E0E0"><?=$AppUI->_('Billing Code Information')?></th>
</tr>
<tr>
	<td align="right"><?=$AppUI->_('Name')?></td>
	<td><input type="text" name="billingcode_name" value="" /></td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_('Value')?></td>
	<td><input type="text" name="billingcode_value" value="" /></td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_('Description')?></td>
	<td><input type="text" name="billingcode_desc" value="" /></td>
</tr>
<tr>
	<td align="center" colspan="2" style="border-top: 1px solid #E0E0E0; padding: 6px">
		<input class="button" type="submit" value="<?=$AppUI->_('Save')?>"/>
		<input class="button" type="button" value="<?=$AppUI->_('Close')?>" onclick="Dialog('idCodeEdit').hide()"/>
	</td>
</tr>
</table>
</form>
</div>

<form name="frmDel" action="index.php?m=system" method="post">
	<input type="hidden" name="dosql" value="do_billingcode_aed" />
	<input type="hidden" name="del" value="1" />
	<input type="hidden" name="company_id" value="<?=$company_id?>" />
	<input type="hidden" name="billingcode_id" value="" />
</form>

